package com.dly.blog.service.impl;

import cn.hutool.db.sql.Direction;
import cn.hutool.db.sql.Order;
import com.dly.blog.content.RedisContent;
import com.dly.blog.domain.Comment;
import com.dly.blog.domain.ro.PageRO;
import com.dly.blog.domain.vo.comment.CommentPageVO;
import com.dly.blog.mapper.CommentMapper;
import com.dly.blog.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dly.blog.utile.RedisUtils;
import com.dly.blog.utile.SecurityUtils;
import org.springframework.stereotype.Service;
import com.dly.blog.domain.vo.comment.CommentVO;
import com.dly.blog.domain.ro.comment.CommentPageRO;
import com.dly.blog.domain.ro.comment.CommentAddRO;
import com.dly.blog.domain.ro.comment.CommentEditRO;
import com.dly.blog.domain.vo.PageVO;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.AllArgsConstructor;
import cn.hutool.core.collection.ListUtil;

import java.util.ArrayList;
import java.util.Set;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author DLY
 * @since 2024-01-30 13:17:33
 */
@Service
@AllArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final RedisUtils redisUtils;

    @Override
    public PageVO<CommentPageVO> getCommentPage(CommentPageRO commentPageRO) {
        //评论分页
        Page<CommentPageVO> commentPage = baseMapper.getCommentPage(commentPageRO.buildIPage(), commentPageRO);
        return PageVO.buildMyBatis(commentPage, comment -> {
            PageRO pageRO = new PageRO();
            pageRO.setCurrent(1);
            pageRO.setSize(2);
            pageRO.setOrders(commentPageRO.getOrders());
            commentPageRO.setParentId(comment.getId());
            Page<CommentPageVO> commentSonPage = baseMapper.getCommentPage(pageRO.buildIPage(), commentPageRO);

            commentSonPage.getRecords().forEach(commentSon ->{
                Set<String> set = redisUtils.getSet(RedisContent.COMMENT_SUPPORT + commentSon.getId());
                commentSon.setSupport(set.contains(SecurityUtils.getUserId()));
            });

            comment.setSonComment(commentSonPage.getRecords());
            comment.setSonCount((int) commentSonPage.getTotal());
            Set<String> set = redisUtils.getSet(RedisContent.COMMENT_SUPPORT + comment.getId());
            comment.setSupport(set.contains(SecurityUtils.getUserId()));
            return comment;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComment(CommentAddRO commentAddRO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddRO, comment);
        baseMapper.insert(comment);
    }

    @Override
    public CommentVO getComment(String id) {
        Comment comment = baseMapper.selectById(id);
        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(comment, commentVO);
        return commentVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeComment(String[] ids) {
        baseMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editComment(CommentEditRO commentEditRO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentEditRO, comment);
        baseMapper.updateById(comment);
    }

    @Override
    public PageVO<CommentPageVO> getCommentSonPage(CommentPageRO commentPageRO) {
        //评论分页
        Page<CommentPageVO> commentPage = baseMapper.getCommentPage(commentPageRO.buildIPage(), commentPageRO);
        return PageVO.buildMyBatis(commentPage, comment -> comment);
    }

}
